package com.example.photo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.photo.common.AuthCheck;
import com.example.photo.common.BaseResponse;
import com.example.photo.common.ErrorCode;
import com.example.photo.constant.WorkStatus;
import com.example.photo.exception.BusinessException;
import com.example.photo.model.VO.WorkVO;
import com.example.photo.model.domain.Work;
import com.example.photo.service.WorkService;
import com.example.photo.utils.ResultUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 审核接口
 *
 * @author Lwh
 * @time 2023/04/09 16:34
 */
@RestController
@RequestMapping("/examine")
public class ExamineController {

    @Resource
    private WorkService workService;

    /**
     * 管理员查询所有未审核的作品
     *
     * @param contestId 查看的赛事id
     */
    @AuthCheck
    @GetMapping("/list")
    public BaseResponse<List<WorkVO>> listWorksToExamine(@RequestParam long pageSize,
                                                         @RequestParam long pageNum,
                                                         @RequestParam Integer contestId) {
        if (contestId < 1) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper<Work> wrapper = new QueryWrapper<>();
        wrapper.eq("is_pass", WorkStatus.NO_EXAMINE);
        wrapper.eq("contest_id", contestId);
        List<Work> list = workService.page(new Page<>(pageNum, pageSize), wrapper).getRecords();
        //为空代表当前无未审核的作品
        if (list.size() == 0) {
            return ResultUtils.success(new ArrayList<>());
        }
        List<WorkVO> workVOS = list.stream().map(WorkVO::new).collect(Collectors.toList());
        return ResultUtils.success(workVOS);
    }

    /**
     * 管理员审核通过作品
     *
     * @param workIds 要通过的作品id列表
     */
    @AuthCheck
    @PostMapping("/pass")
    public BaseResponse<Boolean> passWorks(@RequestBody List<Integer> workIds) {
        if (workIds.size() == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        workService.setIsPass(workIds, WorkStatus.PASS);
        return ResultUtils.success(true);
    }

    /**
     * 管理员审核退回作品
     *
     * @param workIds 要退回的作品id列表
     */
    @AuthCheck
    @PostMapping("/back")
    public BaseResponse<Boolean> backWorks(@RequestBody List<Integer> workIds) {
        if (workIds.size() == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        workService.setIsPass(workIds, WorkStatus.BACK);
        return ResultUtils.success(true);
    }

    /**
     * 管理员查看审核未通过的作品
     *
     * @param contestId 查看的赛事id
     */
    @AuthCheck
    @GetMapping("/back")
    public BaseResponse<List<WorkVO>> listNoPassWorks(@RequestParam long pageSize,
                                                      @RequestParam long pageNum,
                                                      @RequestParam Integer contestId) {
        if (contestId < 1) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper<Work> wrapper = new QueryWrapper<>();
        wrapper.eq("contest_id", contestId);
        wrapper.eq("is_pass", WorkStatus.BACK);
        List<Work> list = workService.page(new Page<>(pageNum, pageSize), wrapper).getRecords();
        if (list.size() == 0) {
            return ResultUtils.success(new ArrayList<>());
        }
        List<WorkVO> workVOS = list.stream().map(WorkVO::new).collect(Collectors.toList());
        return ResultUtils.success(workVOS);
    }

}
